package com.definesys.smecrailtransit.module.Login;

import android.content.Context;

import com.definesys.smecrailtransit.SmecManager.SmecKpManager.SmecKpUtils;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.SmecManager.SmecSession.SmecSession;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.module.Login.bean.CheckUpdateBean;
import com.definesys.smecrailtransit.module.Login.bean.SmecUser;
import com.definesys.smecrailtransit.module.Splash.SplashContract;
import com.github.mzule.activityrouter.router.Routers;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by xupeizuo on 2017/7/25.
 */

public class  LoginPresenter extends SmecBasePresenter implements LoginContract,SplashContract{

    private boolean isAutoLogin=false;

    public LoginPresenter(Context mContext) {
        super(mContext);
    }

    @Override
    public void login(String username, String password) {
        Observable<HttpResult<SmecUser>> observable=smecRailService.login(username, password);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<SmecUser>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                SmecKpUtils.kpDismiss();
                if(isAutoLogin){
                    mRxbus.post(LoginActivity.LOGIN_FAILURE,new Object());
                }
            }

            @Override
            public void onNext(HttpResult<SmecUser> smecUserHttpResult) {
                SmecUser smecUser=smecUserHttpResult.getData();
                smecUser.setPassWord(password);
                _dealWithData(smecUser);
                SmecKpUtils.kpDismiss();
                Routers.open(mContext,"smec://NavigationActivity");
            }
        });
    }

    public void checkUpdate(HashMap<String, String> param) {
        Observable<HttpResult<CheckUpdateBean>> observable=smecRailService.checkUpdate(param);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<CheckUpdateBean>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRxbus.post(LoginActivity.CHANGE_UPDATE_FAILED, "");
            }

            @Override
            public void onNext(HttpResult<CheckUpdateBean> smecUserHttpResult) {
                CheckUpdateBean c = smecUserHttpResult.getData();
                mRxbus.post(LoginActivity.CHANGE_UPDATE_SUCCESS, c);
            }
        });
    }

    @Override
    public void autoLogin() {

        String username=SmecSession.getSmecUser().getUserId();
        String password=SmecSession.getSmecUser().getPassWord();
        if(username == null || username.equals("")
                || password == null ||password.equals("")){
            mRxbus.post(LoginActivity.LOGIN_FAILURE,new Object());
            return;
        }
        isAutoLogin=true;
        login(username,password);
    }

    /**
     * 处理登录数据
     * @param smecUser
     */
    private void _dealWithData(SmecUser smecUser){
        realmManager.deleteRealmObject(SmecUser.class);
        SmecUser smecUserForNew=new SmecUser();
        smecUserForNew.setUserId(smecUser.getUserId());
        smecUserForNew.setUserRole(smecUser.getUserRole());
        smecUserForNew.setToken(smecUser.getToken());
        SmecSession.setToken(smecUser.getToken());
        smecUserForNew.setAutoLogin(true);
        smecUserForNew.setLastLogin(true);
        smecUserForNew.setUserName(smecUser.getUserName());
        smecUserForNew.setPassWord(smecUser.getPassWord());
        smecUserForNew.setRememberAccount(SmecSession.isRememberAccount());
        SmecSession.setSmecUser(smecUserForNew);
        realmManager.insert(SmecSession.getSmecUser());
    }
}

